﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DemoManyTowOne.Domain;
using DemoManyTowOne.IService;
namespace DemoManyTowOne
{
    public partial class frmMain : Form
    {
        Nhibernate _tblConSrv = new Nhibernate();
        public frmMain()
        {
            InitializeComponent();
            LoadGirdView();
        }
        public class ThongTinCon
        {
            public int IDCON { get; set; }
            public String FirtName { get; set; }
            public String LastName { get; set; }
            public string NameCha { get; set; }

        }
        public void LoadGirdView()
        {
            IList<tblCon> list = _tblConSrv.LayThongTinCon();            

            IList<ThongTinCon> hs = new List<ThongTinCon>();
            foreach (var item in list)
            {
                ThongTinCon tt = new ThongTinCon();
                tt.IDCON = item.IDCON;
                tt.FirtName = item.FirtNameCon;
                tt.LastName = item.LastNameCon;
                tt.NameCha = item.ThongTinCha.LastNameCha;

                hs.Add(tt);
            }
            dtgvTest.DataSource = hs;
        }

        private void dtgvTest_TabIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("ok");
        }

        private void dtgvTest_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            ///MessageBox.Show("ok");
        }

        private void dtgvTest_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            MessageBox.Show("ok");
        }

        private void dtgvTest_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            MessageBox.Show("ok");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var a1 = new tblCon
                {
                    IDCON = 18,
                    LastNameCha = "b",
                    FirtNameCon = "thanh",
                    LastNameCon = "c",
                    AgeCon = 23
                };
            ISet<tblCon> con = new HashSet<tblCon>();
            con.Add(a1);
            var cha = new tblCha
            {
                IDCHA = 15,
                FirtNameCha = "them",
                LastNameCha = "xong",
                AgeCha = 50,
                ThongTinCon = con
            };
            _tblConSrv.Sua(cha);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                var a1 = new tblCon
                {
                    FirtNameCon = "Nguyen Quang sadad",
                    LastNameCon = "do thang update",
                    AgeCon = 23,
                    LastNameCha = "b",
                };
                _tblConSrv.ThemCon(a1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
